package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.data.validation.Match;
import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * Class for table Book.
 * @author Elisa
 */
@Entity
public class Book extends Model {

	@Required
	public String title;
	@ManyToOne
	public Publisher publisher;
	@Required
	@ManyToOne
	public Author author;
	@Match("[0-9]{4}")
	public String year;
	@ManyToOne
	public Collection collection;
	public Integer volume;
	public String translation;
	public String category;
	public Integer nbpage;
	
	public Book(final String title, final Author author) {
		this.title = title;
		this.author = author;
	}
	
	
	@OneToMany(mappedBy = "book")
	public List<Cover> covers = new ArrayList<Cover>();
	
	@OneToMany(mappedBy = "book")
	public List<ReadingSheet> sheets = new ArrayList<ReadingSheet>();
	
	
	public String getTitleAndAuthor() {
		return title + " (" + author.getCompletename() + ")";
	}
	
	public String getTitleWithCollectionAndAuthor() {
		String result;
		if (collection == null) {
			result = getTitleAndAuthor();
		} else {
			result = collection.title + " - " + getTitleAndAuthor();
		}
		return result;
	}
	
	
}
